Accessibility

DRK Volume 4 Article

RSSU: RSS Untangled

RSS is a format for syndicating information on the Internet. In order to design an application that uses RSS, you must ask the basic question, "What does RSS stand for?" According to RSS specification version 0.91, RSS 0.92, and RSS 2.0, RSS stands for Really Simple Syndication. But according to RSS 1.0, RSS stands for RDF Site Summary. This naming difference only scratches the surface of the confusion between the competing versions.

When I set off to write an RSS parser I didn't intend to have to untangle anything; I assumed it would be a straightforward process. However, after doing some research I found myself confused as to how these different specifications matched up. In other words, any application that deals with RSS will benefit from a framework that helps you deal with the different formats out there.

RSS Untangled (RSSU) is a core Java API with a ColdFusion layer provided for additional convenience that allows you to parse and generate RSS documents without needing to take into account the discrepancies between versions.

I use the following terms throughout this article:

  • RSS document: Valid XML file that conforms to either RSS 0.91, 0.92, 1.0, or 2.0.
  • RSS style document: RSS document that conforms to versions 0.91, 0.92, or 2.0, where rss is the outermost XML tag.
  • RDF style document: RSS document that conforms to version 1.0, where rdf:RDF is the outermost XML tag.
  • Parse: Process of converting an RSS document into a Channel object (see below).
  • Generate: Process of converting a Channel object to an RSS document. In order to perform this operation, you must specify RDF or RSS as the style.
  • Channel: Generic information about the RSS data you are publishing. A Channel object refers to the Java object that contains this general information. An RSS document must have exactly one Channel.
  • Item: Piece of information you are syndicating, such as an article. An Item object contains this information. An RSS document may contain multiple items.

ColdFusion Installation of RSS Untangled

The RSS Untangled package consists of three installable items. They include the RSS Untangled JAR file, a variety of custom tags, and examples. In the instructions below, wwwroot refers to the web root of your ColdFusion server. Mine is found at C:\CFusionMX\wwwroot. If you use the paths suggested below, all of the examples will function properly. However, if you are an advanced user, feel free to install wherever you feel it's appropriate.

One JAR file is included in this package: rssu.jar contains a Java implementation of an RSS document generator, parser, and document model. Copy rssu.jar to wwwroot/WEB-INF/lib.

You can find the custom tags for RSSU in the tags folder. I recommend that you copy these tags into their own custom tag folder. Install the tags in wwwroot/cf_tags/rssu.

Copy the contents of the components folder to wwwroot/com/macromedia/rssu/.

You should copy the sample folder to a folder under wwwroot. One possible location is wwwroot/rssu-examples.

You can find the source code to the class files found in rssu.jar in com/macromedia/rssu. To compile the source code in Windows, navigate to this directory and run the command:

javac –classpath '../../..' *.java

For other operating systems, the command above may vary.

RSSU requires either Java 1.4 or "Red Sky".

Stand-alone Java Installation of RSS Untangled

RSSU can also be used in a stand-alone Java context without the use of ColdFusion. To install a stand-alone version of RSSU, simply copy rssu.jar to a folder of your choice and include the path to rssu.jar in your CLASSPATH. Complete details on the Java interface can be found in the javadoc. The javadoc can be found in the examples subfolder called javadoc.

The stand-alone Java implementation requires Java 1.4 to run.

Using RSS Untangled

Two fundamental operations are necessary to use the RSSU package: parsing an RSS document into a Channel object and generating an RSS document from a Channel object.

The following two examples demonstrate these concepts in a ColdFusion environment. The code in the subsequent section performs these same operations in a stand-alone Java environment.

Parsing (ColdFusion)

1  <cfimport taglib="/cf_tags/rssu" prefix="rssu"/>
2
3  <rssu:parse url            = "http://www.macromedia.com/go/ccantrell_rss"
4              returnVariable = "channel"/>
5
6  <cfoutput>
7    The Title of the feed is: #channel.getTitle()#
8  </cfoutput>
9
10 <rssu:itemsloop channel = "#channel#">
11    <cfoutput>
12        The title of item #itemIndex# 
13        is #item.getTitle()# <br>
14    </cfoutput>
15 </rssu:itemsloop>

Line 1 sets up the RSSU custom tags. Each RSSU custom tag is given the prefix "rssu" to ensure that it does not clash with the names of any other custom tags. For example, if another custom tag package included a tag named "parse," you could use these two tags simultaneously.

Line 3 downloads and parses the document located at the referenced URL. The parsed values are returned into a variable named "channel." This variable is an instance of the Java object com.macromedia.rssu.Channel.

Line 7 outputs the title of the channel by calling the method getTitle on the Channel object. For a full list of all methods you can invoke on the Channel object, see the javadoc for com.macromedia.rss.Channel.

Line 10 provides convenient access to all of the items contained in this Channel object. While the itemsloop tag is equivalent to calling the method getItems on the Channel object and iterating through each item, itemsloop provides a more convenient interface. Inside of the itemsloop tag, the variables itemIndex and item will always be set to the current item being processed. For example, if you are processing the third item in a sequence of 10, itemIndex will be set to 3 and item will contain an instance of com.macromedia.rssu.Item.

Lines 12–13 access the title of the current item. For further information on the methods that you can invoke on the Item object, see the documentation in examples/javadoc.

Generating (ColdFusion)

1  <cfimport taglib="/cf_tags/rssu" prefix="rssu"/>
2
3  <cfoutput>
4      <rssu:rss returnVariable="channel">
5          <rssu:prop name="title">My Title</rssu:prop>
6          <rssu:prop link="link">#mylink#</rssu:prop>
7          <rssu:dateProp name="pubDate"
8                         value="#now()#"/>
9
10         <rssu:item>
11             <rssu:prop name="link">
12                 #myStoryLink#
13             </rssu:prop>
14             <rssu:prop name="title">
15                  My Story
16             </rssu:prop>
17         </rssu:item>
18     </rssu:rss>
19 </cfoutput>
20
21 <rssu:generate returnVariable = "xml"
22                channel        = "#channel#"
23                style          = "rss"/>
24 <cfoutput>#xmlFormat(xml)#</cfoutput>

As in the previous example, line 1 sets up the RSSU custom tags.

Lines 3–17 build up a Channel object and will be explained below.

Line 19 converts the Channel object into an RSS-style feed and places the text of the XML that was created in the variable named "xml."

Line 22 outputs the actual text of the XML on the page.

In order to generate an RSS document, you need a Channel object that contains information. One way to get such a Channel would be to use the RSSU parse tag to parse a particular document. One application of this technique might be to parse an RSS-style document and generate an RDF-style document, effectively creating an RSS converter. In general, however, you need to construct a Channel object before you can use the generate tag. Lines 3–17 do exactly this.

Line 3 ensures that values surrounded by # marks are evaluated. The tags contained within the cfoutput tag will not actually produce any output.

Line 4 creates an empty Channel object and stores it in the variable specified by the attribute returnVariable. In this case, the variable is named Channel.

Lines 5–6 set values on this Channel object. The prop tag is a general mechanism for setting values on a Channel, Item, or other Java RSSU object. For example, the Channel object has a method named setTitle, which means it has a property named title (notice that the first letter is always lowercase) that you can set using the prop tag. Again, refer to the documentation in examples/javadoc for a full list of properties on a Channel and other objects.

Line 7 performs the same operation as lines 5–6; however, the value that we are setting must be a date rather than a string.

Line 10 constructs a new Item object and automatically attaches this Item object (calls the addItem method) to the Channel we are constructing.

Lines 11–17 perform the same operations as the other prop tags, except they are performing the operation on an Item object as opposed to the Channel object.

Both of these code snippets have corresponding examples that go into much more depth.

A ColdFusion component interface to RSSU is also available; you will find an example of how to use it in the examples folder. Be sure to review the documentation contained inside the components. You can do this most easily by visiting the ColdFusion component file directly in a browser. For example, to view the Generator component’s documentation, you would visit a URL such as http://localhost:8500/com/macromedia/rssu/Generator.cfc.

Parsing (Java)

1   import com.macromedia.rssu.*;
2   ...
3   URL        url     = new URL(...);
4   AutoParser parser  = new AutoParser();
5   Channel    channel = parser.parse(url);
6   System.out.println("title: " + channel.getTitle());

Line 1 imports the RSSU package so that the objects are available for use by their abbreviated names.

Line 3 sets up the URL to the RSS document to be parsed.

Line 4 creates a new AutoParser object. The AutoParser is able to detect the format of RSS being parsed and will delegate to the correct implementation of the parser.

Line 5 parses the document into a Channel using an AutoParser object.

Line 6 prints out the value of the title of the feed.

Generating (Java)

1   Import com.macromedia.rssu.*;
2   ...
3   RssStyleGenerator generator = new RssStyleGenerator();
4
5   Channel channel = new Channel();
6   Channel.setTitle("Your Title");
7   ...
8   
9   String xml = generator.generateToString(channel);
10  System.out.println("Rss Document = \n" + xml);

Line 1 imports the RSSU package so that the objects are available for use by their abbreviated names.

Line 3 sets up a new generator, which will create an RSS style document.

Lines 5–6 create a new Channel object and sets the title to "Your Title."

Line 9 generates the text of an RSS document given a particular Channel.

Line 10 prints this XML to the screen.

Implementing RSS Untangled

At the core of the RSS Untangled package is a Java object model, which has one Java object per RSS document concept. For example, because an RSS document has a Channel, the object model contains a Channel object. Another example is that because an RSS document may contain an image, an Image object is included in the model.

The different properties of each member of the object model are also based on the contents of the RSS document. For example, because an item can have a title and link, the Item object has title and link attributes. All members of the object model extend the class com.macromedia.rssu.RssType and are sometimes referred to as RSS types. Now that this object model is defined, all you need to do in order to implement RSS Untangled is convert an RSS document to an object model, convert an object model to an RSS document, and construct a ColdFusion interface to the object model.

I've already described most of the ColdFusion interface: It consists of a variety of custom tags. Because ColdFusion provides convenient access for dealing with Java objects, you don't need to write very many ColdFusion custom tags. For example, given a Channel object, if you want access to its title, you can simply invoke getTitle on it. You don't need to write any additional interface.

However, in the case of dealing with a list of items, it's easier to write a custom tag to access these items rather than directly iterating through the ArrayList provided by the Channel object. One advantage of the automatic ColdFusion/Java interface is that ColdFusion and Java date objects are interchangeable, allowing operations such as the following to succeed seamlessly:

#second(channel.getPubDate())#

and

#channel.setPubDate(now())#

The process of parsing and generating XML from an object model is complicated by the fact that you have two different styles that you need to work with. In general, the rules for parsing an RDF-style document are similar to—but substantially different from—the rules for parsing an RSS-style document. To cope with this challenge, I made an important design decision. Each property of the object model has an object associated with it that describes how to read from and write itself to XML. These objects are referred to as metadata. Different types of metadata objects describe different ways to read or write data from XML. For example, the ElementMetadata object defines how to read and write data to a tag in XML, while the AttributeMetadata object describes how to read or write data from an attribute. To parse an RSS-style document, you have to attach the correct metadata that describes the format. As a result, the object model is not biased towards any particular format, but can write to or read from any format, provided that you've attached the correct metadata. The object com.macromedia.rssu.RssStyleInstanceFactory defines the metadata necessary to read or write out an RSS-style document. (There is a corresponding object for the RDF-style document.)

Now that metadata describes how to read and write XML, it becomes a simple matter to parse and generate the appropriate XML. The objects RssStyleParser, RssStyleGenerator, RdfStyleParser, and RdfStyleGenerator use metadata objects to perform their respective functions. These objects represent the main entry points into the RSSU Java API.

As a final convenience, the object com.macromedia.rssu.AutoParser examines the XML provided to it, determines whether an RDF-style parser or an RSS-style parser should be used, and delegates to the appropriate object. This provides a nice level of convenience, because it allows you to parse a feed without knowing its type. It is this object that the parse custom tag uses to perform its work.


About the Author

Ben Simon is Director of Systems Engineering at Amazing Media, Inc. He has vast experience programming in Java, perl, scheme, awk, and even a little ColdFusion. When he's not programming, Ben peruses comp.lang.functional or Linux Journal.